<?php
class ActionGenAction extends Action
{
    function _initialize()
    {
        if(Session::get("prj_path") == "")
        {
            $this->assign("jumpUrl",url("listPrj","Project"));
            $this->error("请先选择项目");
        }
    }

    function index()
    {
		$this->assign("pageFile","ActionGen:index");
		$this->layout();
	}

	function save()
	{
		//生成Action文件
		$actionConfig = Array("actionname"=>"","init"=>"off","index"=>"off","show"=>"off","add"=>"off","update"=>"off","delete"=>"off","overwrite"=>"off","initfunc"=>"init","indexfunc"=>"index","showfunc"=>"show","addfunc"=>"add","updatefunc"=>"update","deletefunc"=>"delete");

		foreach($_POST as $key=>$value)
		{
			if(!empty($value))
				$actionConfig[$key] = $value;
		}

		if($actionConfig["actionname"]!="")
		{
			$this->modlename = empty($_POST["modelname"])?$actionConfig["actionname"]:$_POST["modelname"];
			$actfile = Session::get("prj_path")."/Lib/Action/".$actionConfig["actionname"]."Action.class.php";
			$actdir = Session::get("prj_path")."/Tpl/default/".$actionConfig["actionname"];
			$tplcontent = '<tagLib name="cx,html" />';

			if(is_file($actfile) && $actionConfig["overwrite"]!="on")
				$this->error("存在同名的Action文件: ".$actfile);
			
			mk_dir($actdir); //先把对应的模板目录建好

			$content = '<?php
class '.$actionConfig["actionname"].'Action extends Action
{
';

			if($actionConfig["init"]=="on")
				$content .= $this->init();

			if($actionConfig["index"]=="on")
			{
				$content .= $this->pindex($actionConfig["indexfunc"]);
				file_put_contents($actdir."/".$actionConfig["indexfunc"].".html",$tplcontent);
			}

			if($actionConfig["show"]=="on")
			{
				$content .= $this->show($actionConfig["showfunc"]);
				file_put_contents($actdir."/".$actionConfig["showfunc"].".html",$tplcontent);
			}

			if($actionConfig["add"]=="on")
			{
				$content .= $this->add($actionConfig["addfunc"]);
				file_put_contents($actdir."/".$actionConfig["addfunc"].".html",$tplcontent);
			}

			if($actionConfig["update"]=="on")
			{
				$content .= $this->update($actionConfig["updatefunc"]);
				file_put_contents($actdir."/".$actionConfig["updatefunc"].".html",$tplcontent);
			}

			if($actionConfig["insert"]=="on" || $actionConfig["update"]=="on")
				$content .= $this->psave();

			if($actionConfig["delete"]=="on")
				$content .= $this->delete($actionConfig["deletefunc"]);

			$content .= '}
?>';
			file_put_contents($actfile,$content);

			$this->success("Action文件已生成: ".$actfile);
		}
		else
			$this->error("Action名称不能为空");
    }

	private function init()
	{
		$content = '    function _initialize()
    {
        
    }
	
';
		return $content;
	}

	private function pindex($func)
	{
		$content = '	function '.$func.'()
	{
		$Dao = D("'.$this->modlename.'");
        $where = array(); //自己添加一个查询条件吧
		$count = $Dao->count($where);
		import("ORG.Util.Page");
		$listRows = empty($_REQUEST["listRows"])?20:$_REQUEST["listRows"]; //分页时每页显示20条记录
		$p = new Page($count,$listRows);

		$field = "*";
		$list = $Dao->where($where)->field($field)->order("id desc")->limit($p->firstRow.",".$p->listRows)->findAll();
		$page = $p->show();
		$this->assign("list",$list);
		$this->assign("page",$page);
		$this->display();
	}
	
';
		return $content;
	}

	private function show($func)
	{
		$content = '	function '.$func.'()
	{
		$Dao = D("'.$this->modlename.'");
        $where = array("id"=>$_GET["id"]); //自己添加一个查询条件吧
		$field = "*";
		$vo = $Dao->where($where)->field($field)->find();
		$this->assign("vo",$vo);
		$this->display();
	}
	
';
		return $content;
	}

	private function add($func)
	{
		$content = '	function '.$func.'()
	{
		if(!$this->isPost()) // 添加内容的页面
			$this->display();
		else
			$this->save("add");
	}

';
		return $content;
	}

	private function update($func)
	{
		$content = '	function '.$func.'()
	{
		if(!$this->isPost()) // 编辑页面
		{
			$Dao = D("'.$this->modlename.'");
			$vo = $Dao->find("id=".$_GET["id"]);
			if($vo === false)
			{
				$this->assign("jumpUrl",__URL__);
				$this->error("Non-existed record");
			}

			$this->assign("vo",$vo);
			$this->display();
		}
		else
			$this->save("save");
	}

';
		return $content;
	}

	private function psave()
	{
		$content = '	private function save($type="add")
	{
		// 保存添加的数据
		$Dao = D("'.$this->modlename.'");
		$vo = $Dao->create();
		if(false === $vo)
	 		$this->error($Dao->getError());
		
		if($type=="add")
		{
			$rs = $Dao->add();
			$success = "添加数据成功!";
			$error = "添加数据失败";
		}
		else if($type=="save")
		{
			$rs = $Dao->save();
			$success = "修改数据成功!";
			$error = "修改数据失败";
		}

		if($rs)
		{
			$this->assign("jumpUrl",__URL__);
			$this->success($success);
		}
		else
			$this->error($error);
	}

';
		return $content;
	}

	private function delete($func)
	{
		$content = '	function '.$func.'()
	{
		// 根据id删除指定的记录
		$Dao = D("'.$this->modlename.'");
		$id = $_REQUEST["id"];
		if(isset($id))
		{
			$result = $Dao->getById($id);
			if(!$result)
				$this->error("Non-existed record!");

			if($Dao->deleteById($id))
			{
				$this->assign("jumpUrl",__URL__);
				$this->success("删除数据成功");
			}
			else
				$this->error("删除数据失败");
		}
		else
			$this->error("非法操作");
	}
';
		return $content;
	}
}
?>
